// pages/signPage/signPage.js
var app = getApp()
var endTime //签到截止时间
var state //是否开启签到
//var face //是否录入过人脸
var signType  //签到方式
//var status  //学生签到状态

//const status = '0'
//const signType = '2'
//const _state = '1'
//const face = '1'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"Java",
    build_image:"https://sign-face.oss-cn-shanghai.aliyuncs.com/img/test.jpg",
    teacher_username:"wewa",
    courseTime:'16周-20周23453',
    timer:'',//定时器的名字
    clock:'签 到 未 开 启',
    endTime:'0',
    state:'',
    signType:'',
    upload_hidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this

    //课程表：获取截止时间、已开启签到、签到方式
    wx.request({
      method: 'get',
      url: app.globalData.url + 'v1/courses/id/62bce151-fa1f-44ea-bcb2-7c8284f17ae6', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
      },
      success (res) {
        //console.log(res)
        let date = new Date(res.data.courses.endTime)
        
        that.setData({
          endTime : date,
          state: res.data.courses.state,
          signType: res.data.courses.signType,
          courseTime: res.data.courses.courseTime.split(','),
        })
        that.signState(that.data.state);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回上一页
  backlast (){
    wx.navigateBack({
      delta: 3
    })
  },

  //是否已开启签到
  signState:function(state){
    if (state == 1){
      this.countdown();
    } else {
      this.setData({
        clock:'签 到 未 开 启'
      })
    }
  },

  //截止时间倒计时
  countdown:function(){
    let _that = this;
    let minus = _that.timeminus(_that.data.endTime);
    this.setData({
      clock:_that.dateformat(minus)
    })
    if(minus <= 0){
      this.setData({
        clock:'签 到 已 结 束'
      })
      return;
    }
    setTimeout(function(){
      minus-=10;
      _that.countdown()
    },10)
  },
  
  //时间差
  timeminus:function(end_time){
    var date = new Date();
    //var end_time = new Date('2021/06/29 19:30:21');
    var s1 = date.getTime();
    var s2 = end_time.getTime();
    return s2-s1;
  },

  //时间格式化
  dateformat:function(micro_second){
    var second = Math.floor(micro_second/1000);//总秒数
    var day = Math.floor(second/3600/24);//总天数
    var hr = Math.floor(second/3600);//总小时
    var hr2 = hr%24;//小时位
    //second - hr2 = min
    var min = Math.floor((second-hr*3600)/60);//分钟数
    var sec = second-hr*3600-min*60;//秒位
    var micro_sec = Math.floor((micro_second%1000)/10);//毫秒位
    return day+" 天 "+hr2+" 时 "+min+" 分 "+sec+" 秒 "+micro_sec;
  },
  //修改签到状态status=1，已签到
  update_status:function(res){
    app.globalData.records.status = 1
    //console.log(app.globalData.records)
    wx.request({
      method: "post",
      url: app.globalData.url + 'v1/records/update', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: app.globalData.records,
      success (res) {
        console.log(res)
      }
    })
  },
  //修改签到状态status=2，迟到
  update_status_late:function(res){
    app.globalData.records.status = 2
    //console.log(app.globalData.records)
    wx.request({
      method: "post",
      url: app.globalData.url + 'v1/records/update', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: app.globalData.records,
      success (res) {
        console.log(res)
      }
    })
  },
  //上传图片后，将face置1
  update_face:function(res){
    app.globalData.userInfo.face = 1
    console.log(app.globalData.userInfo)
    wx.request({
      method: "post",
      url: app.globalData.url + 'v1/user/update', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: app.globalData.userInfo,
      success (res) {
        console.log(res)
      }
    })
  },
  //上传图片
  upload_image:function(){
    let that = this
    that.setData({
      upload_hidden: true
    })
    console.log("已隐藏上传图片弹框")
    //上传图片
    

    //将face置1
    that.update_face()
  },

  //选择扫码方式
  choose_signWay:function(){
    let that = this
    let minus = that.timeminus(that.data.endTime);
    if (that.data.state == 1) {  //已开启签到
      if (minus >= 0) {  //未到截止时间
        if (that.data.signType == 0){ //自助签到
          //console.log(00)
          that.update_status()
          wx.showModal({    
            content:'签到成功',
            showCancel:false,
            confirmText:'关闭页面',
          })
        } else if(that.data.signType == 1){//扫码签到
          console.log(11)
            wx.navigateTo({
              url: '/pages/payInfo/payInfo',
            })
            }
         else if(that.data.signType == 2){//刷脸签到
          console.log(22)
          if (app.globalData.userInfo.face == 0){
            console.log("上传图片")
            that.setData({
              upload_hidden: false
            })
  
          } else if (app.globalData.userInfo.face == 1){
            console.log("人脸识别")
            //人脸识别接口
  
  
  
  
            //更改签到状态 update_status()
            that.update_status()
            wx.showModal({    
              content:'签到成功',
              showCancel:false,
              confirmText:'关闭页面',
            })
          }
        }
      } else{ //已到截止时间，迟到状态
        that.update_status_late()
        wx.showModal({
          content:'签到成功，您已迟到！',
          showCancel:false,
          confirmText:'关闭页面'
        })
      }
    } else{ //未开启签到
      wx.showModal({
        content:'签到未开启',
        showCancel:false,
        confirmText:'关闭页面'
      })
    }
  }
})